<?php
namespace App\Service;

class CangweiService extends BaseService
{
    //仓位

    //层列表
    public static function getCengList($shelf_id)
    {
        $arys = [];
        $res = self::dbConn(self::$dbCangwei)
            ->select('id', 'title')
            ->where('del', 0)
            ->where('pid', 0)
            ->where('shelf_id', $shelf_id)
            ->get();
        if (!count($res)) {
            return self::toback(0, '', []);
        }
        $arys = [];
        foreach ($res as $v) {
            $subOnes = self::dbConn(self::$dbCangwei)
                ->where('del', 0)
                ->where('pid', $v->id)
                ->get();
            $subList = [];
            foreach ($subOnes as $vv) {
                $subList[] = [
                    'id' => $vv->id, 'title' => $vv->title,
                ];
            }
            $arys[] = [
                'id' => $v->id, 'title' => $v->title,
                'childList' => $subList,
            ];
        }
        return self::toback(0, '', $arys);
    }

    //层添加
    public static function cengAdd($pas)
    {
        $res = (array) self::dbConn(self::$dbCangwei)
            ->where('del', 0)
            ->where('pid', 0)
            ->where('shelf_id', $pas['shelf_id'])
            ->where('title', $pas['title'])
            ->first();
        if (!empty($res)) {
            return self::toback(-2, '存在同名记录');
        }
        $pas['uptime'] = $pas['addtime'] = toDate();
        $res = self::dbConn(self::$dbCangwei)->insertGetId($pas);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }

    //层修改
    public static function cengModify($id, $pas)
    {
        $pas['uptime'] = toDate();
        $res = (array) self::dbConn(self::$dbCangwei)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return self::toback(-2, '记录不存在');
        }
        $pas['uptime'] = toDate();
        $res = self::dbConn(self::$dbCangwei)->insertGetId($pas);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }

    //修改
    public static function add($pas)
    {
        $res = (array) self::dbConn(self::$dbCangwei)
            ->where('pid', $pas['pid'])
            ->where('title', $pas['title'])
            ->first();
        if (!empty($res)) {
            return self::toback(-2, '存在重复记录');
        }
        $pas['uptime'] = $pas['addtime'] = toDate();
        $res = self::dbConn(self::$dbCangwei)->insertGetId($pas);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }

    //修改
    public static function modify($id, $pas)
    {
        $res = (array) self::dbConn(self::$dbCangwei)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return self::toback(-2, '记录不存在');
        }
        $pas['uptime'] = toDate();
        $res = self::dbConn(self::$dbCangwei)
            ->where('id', $id)
            ->update($pas);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }

    //删除
    public static function del($id)
    {
        $pas['deltime'] = toDate();
        $res = (array) self::dbConn(self::$dbCangwei)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return self::toback(-2, '没有数据');
        }
        $pas['del'] = 1;
        $pas['deltime'] = toDate();
        $res = self::dbConn(self::$dbCangwei)
            ->where('id', $id)
            ->update($pas);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }
}